package top.chendawei.main.web.controller;

import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.chendawei.main.facade.UserFacadeImpl;
import top.chendawei.system.beans.entity.SysRole;
import top.chendawei.system.beans.vo.param.RoleAddParam;
import top.chendawei.system.beans.vo.param.RoleParam;
import top.chendawei.system.enums.CommomOperator;
import top.chendawei.system.service.ICommonService;
import top.chendawei.system.util.annotation.SysLog;
import top.chendawei.system.web.controller.AbstractBaseController;
import top.chendawei.util.JsonResult;
import top.chendawei.util.params.ResultMessage;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/sys/role")
@Api(tags = "系统角色")
public class SysRoleController extends AbstractBaseController {
    @Autowired
    private ICommonService commonService;

    @Autowired
    private UserFacadeImpl userFacade;

    @SysLog(description = "查询角色列表")
    @GetMapping({"/page"})
    public ResultMessage page(RoleParam param) {
        initPageParam(param);
        List<String> searchAttr = new ArrayList();
        List<Object> searchVal = new ArrayList();
        List<CommomOperator> searchOper = new ArrayList();
        if (StringUtils.isNoneBlank(param.getNameLike())) {
            searchAttr.add("name");
            searchVal.add(param.getNameLike());
            searchOper.add(CommomOperator.like);
        }
        JsonResult<SysRole> json = this.commonService.search(searchAttr, searchVal, searchOper,
                SysRole.class, param.getCurrent(), param.getPageSize(), param.getOrderBy(), param.getOrderType());
        return ResultMessage.success(json);
    }

    @SysLog(description = "保存角色")
    @PostMapping({"/save"})
    public ResultMessage save(@RequestBody RoleAddParam role) {
        userFacade.saveRole(role);
        return ResultMessage.success();
    }

}
